package com.example.his.api.mis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.his.api.controller.form.SearchDeptByPageForm;
import com.example.his.api.db.dao.DeptDao;
import com.example.his.api.db.pojo.DeptEntity;
import com.example.his.api.exception.HisException;
import com.example.his.api.mis.service.DeptService;
import com.example.his.api.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;

@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements DeptService {


    @Override
    public ArrayList<HashMap> searchAllDept() {
        return baseMapper.searchAllDept();
    }

    @Override
    public PageUtils searchByPage(SearchDeptByPageForm param) {
        Page<HashMap> page = PageHelper.startPage(param.getPage(), param.getLength())
                .doSelectPage(() -> baseMapper.searchByPage(param.getDeptName()));
        return new PageUtils(page.getResult(),page.getTotal(), param.getPage(),param.getLength());
    }

    @Override
    @Transactional
    public int insert(DeptEntity dept) {
        int rows = baseMapper.insert(dept);
        return rows;
    }

    @Override
    public HashMap searchById(int id) {
        HashMap map = baseMapper.searchById(id);
        return map;
    }

    @Override
    @Transactional
    public int update(DeptEntity dept) {
        int rows = baseMapper.update(dept);
        return rows;
    }

    @Override
    @Transactional
    public int deleteByIds(Integer[] ids) {
        if (!baseMapper.searchCanDelete(ids)) {
            throw new HisException("部门下存在员工，无法删除");
        }
        int rows = baseMapper.deleteByIds(ids);
        return rows;
    }
}
